<template>
  <div class="new-topic-container">
    <div class="new-topic">
      <div class="new-topic-title">创作新主题</div>
      <div class="add-title">
        <div class="add-title-text">主题标题</div>
        <div class="add-title-input">
          <input v-model="title" type="text" placeholder="请输入主题标题" />
        </div>
      </div>
      <div class="main-text">
        <div class="main-text-title">正文</div>
        <div class="main-text-input">
          <textarea v-model="content" placeholder="请输入正文"></textarea>
        </div>
      </div>
      <div class="choose-category">
        <div class="choose-text">分类：</div>
        <div class="all-categories">
          <div class="choosed" @click="categoryVisible = !categoryVisible">
            {{ current.name }}
          </div>
          <ul v-show="categoryVisible" class="category-list">
            <li
              v-for="c in categoryList"
              :key="c.id"
              class="list-item"
              @click="choose(c)"
            >
              {{ c.name }}
            </li>
          </ul>
        </div>
      </div>
      <button class="submit" @click="add">发布主题</button>
    </div>
  </div>
</template>

<script>
import { getCategory, addTopic } from '@/api'
import { Toast } from '@/components/Toast'
export default {
  name: 'NewTopic',
  data() {
    return {
      title: '',
      content: '',
      categoryVisible: false,
      current: {
        name: '选择分类',
        id: ''
      },
      categoryList: []
    }
  },

  mounted() {
    this.getCategoryApi()
  },

  methods: {
    async getCategoryApi() {
      const res = await getCategory({ method: 'list' }).catch(err => {
        console.log(err)
      })
      if (res.code === 200) {
        this.categoryList = res.data
      }
    },

    // 选择分类
    choose(c) {
      this.current = {
        name: c.name,
        id: c.id
      }
      this.categoryVisible = false
    },

    // 新增主题
    async add() {
      if (!this.content || !this.title || !this.current.id) {
        Toast({
          type: 'warning',
          text: '请输入标题和内容并选择分类'
        })
        return
      }
      const params = {
        categoryId: this.current.id,
        title: this.title,
        content: this.content
      }
      const res = await addTopic(params).catch(err => {
        console.log(err)
      })
      if (res.code === 200) {
        Toast({
          type: 'success',
          text: '发布成功'
        })
        this.title = ''
        this.content = ''
      }
    }
  }
}
</script>

<style scoped>
.new-topic-container {
  padding: 20px;
}
.new-topic {
  max-width: 980px;
  margin: 0 auto;
  background-color: #fff;
  box-shadow: 0 2px 3px rgba(0, 0, 0, 0.1);
  border-radius: 2px;
}
.new-topic-title,
.main-text-title {
  padding: 10px 15px;
  font-size: 14px;
  border-bottom: 1px solid #e2e2e2;
}
.add-title-text {
  padding: 15px;
  font-size: 14px;
  border-bottom: 1px solid #e2e2e2;
}
.add-title-input {
  padding: 10px 15px;
  background-color: #f9f9f9;
  border-bottom: 1px solid #e2e2e2;
}
.add-title-input input {
  width: 100%;
  line-height: 20px;
  border: none;
  background-color: transparent;
}
.add-title-input input:focus {
  outline: none;
}
.main-text-input {
  padding: 25px 15px;
}
.main-text-input textarea {
  box-sizing: border-box;
  width: 100%;
  height: 340px;
  padding: 5px;
  border: 1px solid #e2e2e2;
  border-radius: 4px;
  font-size: 14px;
}
.submit {
  margin: 0 0 40px 15px;
  padding: 4px 8px;
  color: #333;
  background-color: #fff;
  font-size: 14px;
  border: 1px solid rgba(80, 80, 90, 0.2);
  border-radius: 3px;
  box-shadow: 0 1px 0 rgba(66, 66, 77, 0.1);
  cursor: pointer;
}
.submit:focus {
  outline: none;
}
.submit:active {
  background-color: #e9e9e9;
}
.choose-category {
  display: flex;
  align-items: center;
  padding: 15px;
}
.choose-text {
  font-size: 14px;
}
.all-categories {
  position: relative;
  font-size: 14px;
}
.choosed {
  padding: 5px 10px;
  min-width: 120px;
  line-height: 1;
  border: 1px solid #333;
  border-radius: 3px;
  color: #444;
  cursor: pointer;
}
.category-list {
  position: absolute;
  top: calc(100% + 3px);
  left: 0;
  min-height: 100%;
  background-color: #fff;
  border: 1px solid #333;
  border-radius: 3px;
}
.list-item {
  padding: 5px 10px;
  border-bottom: 1px solid #333;
  cursor: pointer;
}
.list-item:last-child {
  border: none;
}
.list-item:hover {
  background-color: lightgray;
}
</style>
